package com.action.commute;

import com.common.*;
import com.db.commute.*;

import java.text.SimpleDateFormat;
import java.util.*;

public class ListWeekAction {
	private String[] sweek = {"일", "월", "화", "수", "목", "금", "토"};
	private Date prevweek;
	private Date toweek;
	private Date nextweek;
	private int pwnum;
	private int twnum;
	private int nwnum;
	private String swdate = "";
	
	private DateProcess dateProc;
	private ListProcess listProc;
	private CommuteDAO commDAO;
	
	private int sabun;
	private List<Object> listw;
	private List<Float> wtlist = new ArrayList<Float>();
	private List<Integer> latelist = new ArrayList<Integer>();
	
	private int curpage = 1;
	private int lpp = 10;
	private int totalpage;
	
	public int getCurpage() {
		return curpage;
	}
	public void setCurpage(int curpage) {
		this.curpage = curpage;
	}
	public int getTotalpage() {
		return totalpage;
	}
	public void setLpp(int lpp) {
		this.lpp = lpp;
	}
	
	public List<Float> getWtlist() {
		return wtlist;
	}
	public List<Integer> getLatelist() {
		return latelist;
	}
	
	public void setSwdate(String swdate) {
		this.swdate = swdate;
	}
	public List<Object> getListw() {
		return listw;
	}
	public void setSabun(int sabun) {
		this.sabun = sabun;
	}
	
	public void setDateProc(DateProcess dateProc) {
		this.dateProc = dateProc;
	}
	public void setCommDAO(CommuteDAO commDAO) {
		this.commDAO = commDAO;
	}
	public void setListProc(ListProcess listProc) {
		this.listProc = listProc;
	}
	
	public String[] getSweek() {
		return sweek;
	}
	public Date getToweek() {
		return toweek;
	}
	public void setToweek(Date toweek) {
		this.toweek = toweek;
	}
	public Date getPrevweek() {
		return prevweek;
	}
	public Date getNextweek() {
		return nextweek;
	}
	
	public int getPwnum() {
		return pwnum;
	}
	public int getTwnum() {
		return twnum;
	}
	public int getNwnum() {
		return nwnum;
	}
	
	public String execute(){
		String result = "";
		try {			
			if(swdate.length() == 0){
				toweek = new Date();
			}else{
				toweek = dateProc.valueToDate(swdate);			
			}
			Date weeks[] = dateProc.getWeeks(toweek);
			// 이전, 오늘, 다음 날짜를 구한다
			prevweek = weeks[0];
			toweek = weeks[1];
			nextweek = weeks[2];
			//System.out.println(prevweek+"|"+toweek+"|"+nextweek);
			int wnums[] = dateProc.getWeekNums(toweek);
			// WEEK_OF_YEAR 값을 별도로 보낸다(출력용)
			pwnum = wnums[0];
			twnum = wnums[1];
			nwnum = wnums[2];
			//System.out.println(pwnum+"|"+twnum+"|"+nwnum);
			
			List<Integer> sblist = commDAO.getSabunList();
			listw = new ArrayList<Object>(sblist.size());
			Date oneweek[] = dateProc.getOneWeek(toweek);
			// 각 사번마다 해당주간의 시작일~끝나는날 까지의 출퇴근 목록을 얻어와 저장한다
			for(Integer i : sblist){
				// 사번별로 출퇴근 목록을 얻기 위한 인자로 VO사용
				CommuteVO cvo = new CommuteVO();
				cvo.setSabun(i.intValue());
				cvo.setCwork(oneweek[0]);
				cvo.setLwork(oneweek[2]);
                cvo.setRegdate(toweek);				
				List<CommuteVO> cwlist = commDAO.getListWeek(cvo);
				// 얻어온 리스트를 1~말일까지의 월 크기에 맞춰 중간에 공백VO를 삽입한다
				cwlist = dateProc.convertList(i.intValue(), toweek, cwlist);
				float wttotal = 0;
				int latecount = 0;
				for(int j=0; j<cwlist.size(); j++){
					// 근무시간 합산과 지각횟수 구하기
					wttotal += cwlist.get(j).getWorktime();
					int sttime = 900;	//기준 시간 9시
					SimpleDateFormat sdf = new SimpleDateFormat("hhmm");
					// NullPointerException방지 (cwork값이 null이 아닐경우에만 수행)
					if(cwlist.get(j).getCwork() != null){						
						String strtime = sdf.format(cwlist.get(j).getCwork());
						int ctime = Integer.parseInt(strtime);
						if(ctime > sttime){
							latecount++;
						}
					}
				}				
				wtlist.add(wttotal);
				latelist.add(latecount);
				// 전체 리스트에 add한다 (사번별 리스트안에 출퇴근 목록 리스트 2중 구조)
				listw.add(cwlist);
			}	
			// dateProc.getOneWeek(toweek);
			// 페이지 구별
			totalpage = listProc.getTotalPage(listw, lpp);
			listw = listProc.createPage(curpage, lpp, totalpage, listw);
			result = "success";
		} catch (Exception e) {
			System.out.println("ListWeekAction.execute():"+e.toString());
			e.printStackTrace();
			result = "error";
		}
		return result;
	}
}